unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus;

type

  TLoginInfo = record
    Server: string;
    Host: string;
    Service: string;
    Protocol: string;
    User: string;
    Password: string;
    Database: string;
  end;

  TfrmMain = class(TForm)
    MainMenu1: TMainMenu;
    mmiFile: TMenuItem;
    mmiHelp: TMenuItem;
    mmiAbout: TMenuItem;
    mmiRun: TMenuItem;
    mmiSeparator01: TMenuItem;
    mmiExit: TMenuItem;
    procedure mmiRunClick(Sender: TObject);
    procedure mmiExitClick(Sender: TObject);
    procedure mmiAboutClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    procedure InitConnect(LoginInfo: TLoginInfo);
    procedure ShowData;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

uses LoginParams, DBData, DM, About;

{$R *.DFM}

const

  szSERVER = 'INFORMIXSERVER'; { do not localize }
{$IFDEF WIN32}
  szHOST = 'WIN32HOST'; { do not localize }
  szPROTOCOL = 'WIN32PROTOCOL'; { do not localize }
  szSERVICE = 'WIN32SERVICE'; { do not localize }
  szUSER = 'WIN32USER'; { do not localize }
  szPASSWORD = 'WIN32PASS'; { do not localize }
{$ELSE}
  // Linux
  szHOST = 'HOST'; { do not localize }
  szPROTOCOL = 'PROTOCOL'; { do not localize }
  szSERVICE = 'SERVICE'; { do not localize }
  szUSER = 'USER'; { do not localize }
  szPASSWORD = 'PASS'; { do not localize }
{$ENDIF}

  szDATABASE = 'DATABASE'; { do not localize }

procedure TfrmMain.InitConnect(LoginInfo: TLoginInfo);
begin
  frmDM.IfxConnection.Connected := False; 
  with frmDM.IfxConnection.Params, LoginInfo do
  begin
    Values[szSERVER] := Server;
    Values[szHOST] := Host;
    Values[szSERVICE] := Service;
    Values[szPROTOCOL] := Protocol;
    Values[szUSER] := User;
    Values[szPASSWORD] := Password;
    Values[szDATABASE] := Database;
  end;
end;

procedure TfrmMain.ShowData;
var
  frmDBData: TfrmDBData;
begin
  frmDBData := TfrmDBData.Create(Self);
  frmDM.IfxQuery.Open;
  try
    frmDBData.ShowModal;
  finally
    frmDM.IfxQuery.Close;
    frmDBData.Free;
  end;
end;

procedure TfrmMain.mmiRunClick(Sender: TObject);
var
  frmLoginParams: TfrmLoginParams;
  LoginInfo: TLoginInfo;
begin
  frmDM.IfxConnection.GetInformixEnvironment(frmDM.IfxConnection.Params);

  frmLoginParams := TfrmLoginParams.Create(Self);
  try
    frmLoginParams.edServer.Text := frmDM.IfxConnection.Params.Values[szSERVER];

    if frmLoginParams.ShowModal = mrOk then
      with LoginInfo, frmLoginParams do
      begin
        Server := edServer.Text;
        Host := edHost.Text;
        Service := edServiceName.Text;
        Protocol := edProtocolName.Text;
        User := edUserName.Text;
        Password := edPassword.Text;
        Database := edDatabase.Text;

        InitConnect(LoginInfo);
        ShowData;
      end;
  finally
    frmLoginParams.Free;
  end;
end;

procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if MessageDlg(' Closing Session ', mtConfirmation, [mbYes, mbNo], 0) = mrNo then
    CanClose := False;
end;

procedure TfrmMain.mmiExitClick(Sender: TObject);
begin
  Close;
end;


procedure TfrmMain.mmiAboutClick(Sender: TObject);
var
  frmAboutBox: TfrmAboutBox;
begin
  frmAboutBox := TfrmAboutBox.Create(Self);
  try
    frmAboutBox.ShowModal;
  finally
    frmAboutBox.Free;
  end;
end;


end.

